<?php namespace xlx;

class Proc
{ 
    public static function getEventLoop()
    { 
        if (empty(\Workerman\Worker::$globalEvent)) {
            \Workerman\Worker::$globalEvent = new Client\WaitEvent(); 
        }
        return \Workerman\Worker::$globalEvent;
    }
    public static function self(){
        foreach (debug_backtrace() as $next) {
            if (isset($next['object']->__PID__)) {
                return  $next['object']->__PID__;
            }   
        } 
        throw new \Exception("nonsupport php-cgi/php-fpm process", 1); 
    }
    public static function send($pid, $data)
    {
        if (!\preg_match('/([\d\.]+:\d+):(\d+)/', $pid, $arr)) {
            throw new \Exception("Error Processing Request", 1);
        } 
        return \xlx\TcpPool::postData($arr[1],[
            'act'=>'send','pid'=>$pid,'msg'=>$data 
        ]);
    }
 
    public static function link($pid){ 
        if (!\preg_match('/([\d\.]+:\d+):(\d+)/', $pid, $arr)) {
            throw new \Exception("Error Processing Request", 1);
        } 
        return \xlx\TcpPool::postData($arr[1],[
            'act'=>'link','pid'=>$pid,'link'=>self::self() 
        ]);
    }  
    public static function register($name,$pid){
        return \xlx\TcpPool::postData(\xlx\TcpPool::$addr,[ 'act'=>'register', 'name'=>$name,'pid'=>$pid ], true);  
    }    
    public static function unregister($name,$pid){
        return \xlx\TcpPool::postData(\xlx\TcpPool::$addr,[ 'act'=>'unregister', 'name'=>$name,'pid'=>$pid ], true);  
    }
    public static function whereis($name){
        return \xlx\TcpPool::postData(\xlx\TcpPool::$addr,[ 'act'=>'whereis', 'name'=>$name ], true); 
    }
  
 

    // const trap_exit=1;
    // const error_handler=1;
    // const min_heap_size=1;
    // const min_bin_vheap_size=1;
    // const max_heap_size=1;
    // const message_queue_data=1;
    // const priority=1; 
    // public static function flagTrapExit($bool){

    // }




}
